package com.meng.system.controller.job;

import com.meng.system.constant.SystemConstant;
import com.meng.system.domain.dto.SysJobDTO;
import com.meng.system.domain.vo.ResponseVO;
import com.meng.system.enums.ErrorEnum;
import com.meng.system.runnable.SchedulingRunnable;
import com.meng.system.service.job.JobService;
import com.meng.system.config.CronTaskRegistrar;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * @author Mr.Still
 * @version 1.0.0
 * @date 2021-06-13 00:10
 **/
@Api(tags = "可视化JOB")
@Slf4j
@RestController
@RequestMapping("/job")
public class JobController {

    @Autowired
    private JobService jobService;

    @Autowired
    private CronTaskRegistrar cronTaskRegistrar;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @ApiOperation(value = "查询接口")
    @GetMapping(value = "/findJobMsgList")
    public ResponseVO<?> findJobMsgList(@RequestParam("status")Integer status,
                                        @RequestParam(required = false, defaultValue = "1", value = "pageNum") Integer pageNum,
                                        @RequestParam(required = false, defaultValue = "10", value = "pageSize") Integer pageSize){
        return ResponseVO.result(ErrorEnum.SUCCESS,jobService.findJobMsgList(status,pageNum,pageSize));
    }

    @ApiOperation(value = "添加job")
    @PostMapping(value = "/addRunJob")
    public ResponseVO<?> addRunJob(@RequestBody SysJobDTO job){
        boolean flag = jobService.addRunJob(job);
        if (flag){
            return ResponseVO.result(ErrorEnum.SUCCESS,flag);
        }else {
            if (job.getJobStatus().equals(SystemConstant.NORMAL)) {
//                SchedulingRunnable task = new SchedulingRunnable(job.getBeanName(), job.getMethodName(), job.getMethodParams());
                SchedulingRunnable task = new SchedulingRunnable(job.getBeanName(), job.getMethodName());
                cronTaskRegistrar.addCronTask(task, job.getCronExpression());
            }
            return ResponseVO.result(ErrorEnum.JOB_SAVE_RUN_FAIL);
        }
    }

    @ApiOperation(value = "删除运行时job")
    @PostMapping(value = "/delRunJob")
    public ResponseVO<?> delRunJob(@RequestBody SysJobDTO job){
        boolean success = jobService.deleteSysJobById(job.getJobId());
        if (!success) {
            return ResponseVO.result(ErrorEnum.JOB_SAVE_RUN_FAIL);
        } else{
            if (job.getJobStatus().equals(SystemConstant.NORMAL)) {
                SchedulingRunnable task = new SchedulingRunnable(job.getBeanName(), job.getMethodName(), job.getMethodParams());
                cronTaskRegistrar.removeCronTask(task);
            }
        }

        return ResponseVO.result(ErrorEnum.SUCCESS,success);
    }

    @ApiOperation(value = "启动or停止job")
    @PostMapping(value = "/startOrStopRunJob")
    public ResponseVO<?> startOrStopRunJob(@RequestBody SysJobDTO job){
        //启动或者 停止job
        if (job.getJobStatus().equals(SystemConstant.NORMAL)) {
            SchedulingRunnable task = new SchedulingRunnable(job.getBeanName(), job.getMethodName(), job.getMethodParams());
            cronTaskRegistrar.addCronTask(task, job.getCronExpression());
            return ResponseVO.result(ErrorEnum.JOB_RUN_START_SECCUSS);
        } else {
            SchedulingRunnable task = new SchedulingRunnable(job.getBeanName(), job.getMethodName(), job.getMethodParams());
            cronTaskRegistrar.removeCronTask(task);
            return ResponseVO.result(ErrorEnum.JOB_RUN_STOP_SECCUSS);
        }
    }

    @ApiOperation(value = "修改job")
    @PostMapping(value = "/updateRunJob")
    public ResponseVO<?> updateRunJob(@RequestBody SysJobDTO job){
        boolean success = jobService.updateJob(job);
        if (!success) {
            return ResponseVO.result(ErrorEnum.JOB_RUN_UPDATE_FAIL);
        } else {
            //先移除再添加
            if (job.getJobStatus().equals(SystemConstant.NORMAL)) {
                SchedulingRunnable task = new SchedulingRunnable(job.getBeanName(), job.getMethodName(), job.getMethodParams());
                cronTaskRegistrar.removeCronTask(task);
//                SchedulingRunnable task1 = new SchedulingRunnable(job.getBeanName(), job.getMethodName(), job.getMethodParams());
                cronTaskRegistrar.addCronTask(task, job.getCronExpression());
            }
        }
        return ResponseVO.result(ErrorEnum.JOB_RUN_UPDATE_SECCUSS);
    }
}
